//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    movies: [
    ] ,
    display: "none",
    cate:'',
    thimg:[
      // { pic: '../image/list_09_03.png', url:'../superApp/superApp'},
      // { pic: '../image/list_10_03.png', url: '../super/super' },
    ]
    
  },
  saoma:function(){
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
      }
    })
  },
  onShow:function(){
    let that=this;

    var user = wx.getStorageSync('userInfo').uid;
    var address = app.data.address;
    // wx.getLocation({
    //   type: 'wgs84',
    //   success: function (res) {
    //     var latitude = res.latitude
    //     var longitude = res.longitude
    //     wx.request({
    //       url: app.data.Url + 'wisdom/get_add',
    //       data: {
    //         latitude: latitude,
    //         longitude: longitude
    //       },
    //       method: 'post',
    //       success: function (res) {
    //         console.log(res.data.data.bid, 'res.data.data')
    //         app.data.address.name = res.data.data.name
    //         app.data.address.id = res.data.data.bid
    //         that.setData({
    //           address: res.data.data.name,
    //           bid: res.data.data.bid
    //         })
    //         that.ajax()
    //       }
    //     })
    //   }
    // })
    //address.id = 
    if (address.id == '' || address.id == null || address.id == undefined){
      wx.request({
        url: app.data.Url + 'user/get_shop',
        data: {
          uid: wx.getStorageSync('userInfo').uid
        },
        method: 'get',
        success: function (res) {
          if (res.data.data.id == '' || res.data.data.id == undefined || res.data.data.id == null) {
            wx.getLocation({
              type: 'wgs84',
              success: function (res) {
                var latitude = res.latitude
                var longitude = res.longitude
                wx.request({
                  url: app.data.Url + 'wisdom/get_add',
                  data: {
                    latitude: latitude,
                    longitude: longitude
                  },
                  method: 'post',
                  success: function (res) {
                    console.log(res.data.data.bid, 'res.data.data')
                    app.data.address.name = res.data.data.name
                    app.data.address.id = res.data.data.bid
                    that.setData({
                      address: res.data.data.name,
                      bid: res.data.data.bid
                    })
                  that.ajax()
                  }
                })
              }
            })
            console.log('true')
          } else {
            //绑定社区
            app.data.address.name = res.data.data.name
            app.data.address.id = res.data.data.id
            console.log(app.data.address, '=============')
            that.setData({
              address: res.data.data.name,
              bid: res.data.data.id
            })
           that.ajax();
          }
        }
      })
    } 
    console.log(app.data.address)
    that.setData({
      address: app.data.address.name
    })
    that.ajax();
  },
  onLoad: function () {
    var that = this;

    console.log(app.data.address.id)
    if (!wx.getStorageSync('userInfo')) wx.hideTabBar({
    });
    wx.setStorageSync('classlist','')
    if (!wx.getStorageSync('userInfo')) {
      that.setData({
        logo: true
      })

    }
  //   wx.request({
  //     url: app.data.Url + 'banner/banlist',
  //     data: {
  //       bid: 123,
  //       type: 0
  //     },
  //     success: function (res) {
  //       that.setData({
  //         movies: res.data.data
  //       })
  //     }
  //   })
  },

  //跳转
  onurl:function(e){
    app.navTo(e.currentTarget.dataset.url + '?title=' + e.currentTarget.dataset.title)
  },

  sendRequest: function (res) {
    let that = this;
    if (!res.detail.rawData) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      wx.hideLoding()
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    wx.login({
      success: function (code) {
        var code = code.code;
        wx.request({
          url: app.data.Url + 'User/logins',
          data: {
            utoken: '',
            code: code,
            encryptedData: res.detail.encryptedData,
            iv: res.detail.iv,
            bid: 123
          },
          method: 'post',
          success: function (rs) {
            if (rs.data.success == 10) {
              wx.request({
                url: app.data.Url + 'User/getUid',
                data: {
                  openid: rs.data.openid
                },
                method: 'post',
                success: function (e) {
                  console.log('请求成功')
                  // if (!e.data.data) 
                  // wx.showModal({
                  //   title: '提示',
                  //   content: '请重新点击授权2',
                  // })
                  // wx.hideLoading()
                  wx.setStorageSync('userInfo', {
                    head: res.detail.userInfo.avatarUrl,
                    name: res.detail.userInfo.nickName,
                    sex: res.detail.userInfo.gender,
                    uid: e.data.data,
                    openid: rs.data.openid
                  })
                  wx.request({
                    url: app.data.Url + 'user/get_shop',
                    data: {
                      uid: wx.getStorageSync('userInfo').uid
                    },
                    method: 'get',
                    success: function (res) {
                      if (res.data.data.id == '' || res.data.data.id == undefined || res.data.data.id == null) {
                        wx.getLocation({
                          type: 'wgs84',
                          success: function (res) {
                            var latitude = res.latitude
                            var longitude = res.longitude
                            wx.request({
                              url: app.data.Url + 'wisdom/get_add',
                              data: {
                                latitude: latitude,
                                longitude: longitude
                              },
                              method: 'post',
                              success: function(res){
                                console.log(res.data.data.bid,'res.data.data')
                                app.data.address.name = res.data.data.name
                                app.data.address.id = res.data.data.bid
                                that.setData({
                                  address: res.data.data.name,
                                  bid: res.data.data.bid
                                })
                                that.ajax()
                              }
                            })
                          }
                        })
                        console.log('true')
                      } else {
                        //绑定社区
                        app.data.address.name = res.data.data.name
                        app.data.address.id = res.data.data.id
                        console.log(app.data.address, '=============')
                        that.setData({
                          address: res.data.data.name,
                          bid: res.data.data.id
                        })
                        that.ajax() 
                      }
                    }
                  })
                  
                  //请求绑定社区
                  wx.setStorageSync('phone', e.data.phone)
           
                  wx.showTabBar({
                    success: function () {
                      that.setData({
                        logo: false
                      })
                      wx.showTabBar({
                        
                      })
                      wx.hideLoading()
                    }
                  })

                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '请重新点击授权',
              })
              wx.hideLoading()
              return false;
            }
          }
        })
      }
    })
  },
  clickurl: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index
    // wx.setStorageSync('classlist',id)
    wx.switchTab({
      url: '../classify/classify',
    })

    wx.setStorage({
      key:"tid",
      data:id,
    })
    // wx.navigateTo({
    //   url: '../assortment/assortment?id=' + id,
    // })
  },
  search:function(){
    wx.navigateTo({
      url: '../search/seller',
    })
  },
  onShareAppMessage:function(){
    
  },

  //请求数据
  ajax:function(){
    let that = this;
    that.setData({
      movies:[],
      cate:[],
      goods:[],
    })
    //首页两张图片
    wx.request({
      url: app.data.Url + 'banner/banneroddslist',
      data: {
        bid: app.data.address.id,
      },
      method: 'get',
      success: function (res) {
        that.setData({
          thimg: res.data.data
        })
      }
    })
    wx.request({
      url: app.data.Url + 'banner/Characters',
      data: {
        bid: app.data.address.id,
      },
      method: 'post',
      success: function (res) {
        that.setData({
          Title: res.data.msg
        })
      }
    })
    //2.请求页面剩余数据
    wx.request({
      url: app.data.Url + 'menus/menus',
      data: {
        // bid:123
        bid: app.data.address.id
      },
      method: 'get',
      success: function (res) {
        that.setData({
          cate: res.data.data
        })
      }
    })
    //
    wx.request({
      url: app.data.Url + 'goods/ifyAndgoods',
      data: {
        bid: app.data.address.id,
        limit: 4
      },
      method: 'get',
      success: function (res) {
        var data = []
        if (res.data.data == ''){

        }else{
          data = res.data.data
          // for (var i = 0; i < 4; i++) {
          //   data.push(res.data.data[i])
          // }
          console.log(data)
          that.setData({
            // quality: res.data.data
            goods: data
          })
        }
        
        //console.log(res.data.data)
      }
    })
    //
    wx.request({
      url: app.data.Url + 'banner/banlist',
      data: {
        bid: app.data.address.id,
        type: 0
      },
      success: function (res) {
        that.setData({
          movies: res.data.data
        })
      }
    })
  },

  //banne跳转
  bannerurl:function(e){
    let that =this;
    console.log(e.currentTarget.dataset.id)
    console.log(e.currentTarget.dataset.goodsid)
    if (e.currentTarget.dataset.id != 0 && e.currentTarget.dataset.goodsid != 0){
      wx.navigateTo({
        url: '../collageWare/collageWare?id=' + e.currentTarget.dataset.id + '&goodsid=' + e.currentTarget.dataset.goodsid,
      })
    } else if(e.currentTarget.dataset.goodsid){
      wx.navigateTo({
        url: '../normal/normal?id=' + e.currentTarget.dataset.goodsid,
      })
    }
  },
  //社区跳转
  nav_sq:function(){
    wx.navigateTo({
      url: '../choseAddress/choseAddress',
    })
  }


})
